<?php

namespace App\Http\Controllers;

use App\Article;
use App\Comment;
use Illuminate\Http\Request;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $info = Article::orderBy('id', 'desc')->withCount('comments')->paginate(3);
        return view('Article.index', compact('info'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('Article.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if ($request->hasFile('pic') && $request->file('pic')->isValid()) {
            $filePath = upload($request, 'pic', 'images/' . date('Ymd'));
            $thumb_name = thumb($filePath);
            $request->offsetSet('title_img', '/storage/' . $filePath);
            $request->offsetSet('thumb_img', $thumb_name);
        }
        $this->validate($request, [
            'title' => 'bail|required|unique:articles|between:5,50',
            'content' => 'bail|required'
        ]);
//        $article = new Article();
//        $article->title = $request->title;
//        $article->content = $request->input('content');//get('content') or all(['content'])[0]
//        $article->user_id = 1;
//        $article->save();
        $request->offsetSet('user_id', \Auth::id());
        Article::create($request->all(['title', 'intro', 'title_img', 'thumb_img', 'content', 'user_id']));
        return redirect('article')->with('success', '提交成功');
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show(Article $article)
    {
        $article = Article::withCount('comments')->find($article->id);
//        dump($article);
//        dd($article->comments);
        return view('Article.show', compact('article'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Article $article)
    {
        return view('article.edit', compact('article'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Article $article)
    {
        if ($request->hasFile('pic') && $request->file('pic')->isValid()) {
            $filePath = upload($request, 'pic', 'images/' . date('Ymd'));
            $thumb_name = thumb($filePath);
            $request->offsetSet('title_img', '/storage/' . $filePath);
            $request->offsetSet('thumb_img', $thumb_name);
        }
        $this->validate($request, [
            'title' => 'bail|required|between:5,50|updateOnly:' . $article->title,
            'content' => 'bail|required'
        ]);
        $request->offsetSet('user_id', 1);
        $data = $request->all();
//        unset($data['s']);
        unset($data['pic']);
        $article->update($data);
        return redirect('article')->with('success', '提交成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Article $article)
    {
        $article->delete();
        return redirect('article')->with('success', '文章删除成功');
    }

    public function comment(Request $request, Article $article)
    {
        $this->validate($request, [
            'content' => 'required'
        ]);
        $comment = new Comment();
        $comment->user_id = \Auth::id();
        $comment->article_id = $article->id;
        $comment->content = $request->input('content');
        //关联保存
        $article->comments()->save($comment);

        return redirect('/article/' . $article->id)->with('message', '发表成功');
    }
}
